
import ij.IJ;
import ij.ImagePlus;
import ij.WindowManager;
import ij.gui.GenericDialog;
import ij.plugin.filter.PlugInFilter;
import ij.process.ImageProcessor;
import ij.text.TextWindow;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;



/**
 *
 * @author Michalis Dobekidis
 */
public class MeanProcessor_ implements PlugInFilter
{
     private int       boxWidth,
                       boxHeight,
                       boxStepX,boxStepY;
  TextWindow textLog;

  String finalString = "Feature Vector"+"\n"+"\n";
  public BufferedImage betaImg;
  public BufferedImage IMFImage;
  


  public int setup(String arg, ImagePlus imp)
  {
        imp = WindowManager.getCurrentImage();
        return DOES_8G;
  }


      public void run(ImageProcessor ip)
  {
        // Default values
        boxWidth    = 24;
        boxHeight   = 24;
        boxStepX    = 1;
        boxStepY    = 1;
        ImagePlus imp = WindowManager.getCurrentImage();
        //getting the area of interest that the user might have choosen
        Rectangle areaOfInterest = imp.getProcessor().getRoi();
        //if(areaOfInterest.width!=boxWidth&&areaOfInterest.width!=0)boxWidth=areaOfInterest.width;
        //if(areaOfInterest.height!=boxHeight&&areaOfInterest.height!=0)boxHeight=areaOfInterest.height;

        // Creates a new window on ImageJ where text, or GUI elements are added.
        GenericDialog gd = new GenericDialog("Feature Vector sampler");
        gd.addMessage("Sampling box preferences: ");
        gd.addNumericField("Width of the Box", boxWidth,1);
        gd.addNumericField("Height of the Box", boxHeight,1);
        gd.addNumericField("Box pacing on X-axis", boxStepX,1);
        gd.addNumericField("Box pacing on Y-axis", boxStepY,1);

        gd.showDialog();
        if(gd.wasCanceled())
        {
            IJ.error("Plugin canceled");
            return;
        }

        // Get GUI values
        boxWidth = (int)gd.getNextNumber();
        boxHeight = (int)gd.getNextNumber();
        boxStepX = (int)gd.getNextNumber();
        boxStepY = (int)gd.getNextNumber();
        if(gd.invalidNumber())
        {
            IJ.error("Invalid field");
            return;
        }
    
       // FileInfo fi = imp.getOriginalFileInfo();
        imp.updateAndDraw();

        WindowScanner featureUtil = new WindowScanner();

        //Start sliding window with inputs from dialog window.
       int[][][][] featureArray = featureUtil.slidingWindow(imp.getProcessor(),boxWidth,boxHeight,boxStepX,boxStepY,areaOfInterest);

      
       featureUtil.calculateMean(featureArray, boxWidth, boxHeight, featureArray[0].length, featureArray.length,boxStepX,boxStepY,imp.getProcessor().getWidth(),imp.getProcessor().getHeight());

       return;
    }


}
